from PySide6.QtCore import QEvent, QObject
from PySide6.QtGui import QShowEvent, QMouseEvent, QPaintEvent, QPainter, QCursor
from mainUi import *
import os, sys, time

TITLE_NAME_VER = "Tank地图编辑器 V1.0"


class MainWindow(QWidget):
    def __init__(self, parent=None):
        super().__init__()
        # self.resize(30 * 22 + 160, 30 * 22 + 100)
        self.setWindowTitle(TITLE_NAME_VER)
        self.setWindowIcon(QIcon(":icon/logo.png"))
        self.setAttribute(Qt.WA_TranslucentBackground)
        self.setWindowFlags(Qt.FramelessWindowHint | Qt.Window)

        # 窗口标记
        self.m_flag = False

        # 标题栏
        p = QLabel()
        p.setPixmap(QPixmap(":icon/logo.png").scaled(24, 24))
        t = QLabel(TITLE_NAME_VER)
        t.setStyleSheet("color:white;")
        self.toolbar = QToolBar()
        self.toolbar.setIconSize(QSize(16, 16))
        self.toolbar.addAction(
            QAction(
                QApplication.style().standardIcon(QStyle.SP_TitleBarMinButton),
                "最小化",
                self,
            )
        )
        self.toolbar.addAction(
            QAction(
                QApplication.style().standardIcon(QStyle.SP_TitleBarMaxButton),
                "最大化",
                self,
            )
        )
        self.toolbar.addAction(
            QAction(
                QApplication.style().standardIcon(QStyle.SP_TitleBarCloseButton),
                "关闭",
                self,
            )
        )
        h0 = QHBoxLayout()
        h0.setContentsMargins(0, 0, 0, 0)
        h0.addWidget(p)
        h0.addWidget(t)
        h0.addStretch(1)
        h0.addWidget(self.toolbar)

        self.Xbox = MainChangeWindow()
        # 顶层
        v = QVBoxLayout(self)
        v.setContentsMargins(5, 5, 5, 15)
        v.addLayout(h0)
        v.addWidget(self.Xbox)

        # css
        self.setStyleSheet(
            """
        MainWindow {background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1, stop:0 rgba(31,31,31, 255), stop:1 rgba(61, 61, 61, 255)); border-radius: 5px;}        
        """
        )

        # 信号槽
        self.toolbar.actionTriggered.connect(self.MSG_SYSTEM)
        self.Xbox.soap.connect(self.MSG_output)
        # 重画窗口大小
        hint = self.Xbox.room.viewportSizeHint()
        self.resize(hint.width() + 62, hint.height() + 55)
        # 剪贴板
        self.clipboard = QApplication.clipboard()

    def MSG_output(self, e):
        """输出数据"""
        txt = "[\n"
        for i in e:
            txt += f"{i},\n"
        txt += "]\n"
        self.clipboard.setText(txt)
        txt += "数据已经复制到剪贴板上"
        QMessageBox.about(self, "数据输出", txt)

    def MSG_SYSTEM(self, e):
        """系统工具栏消息"""
        if e.text() == "最小化":
            self.showMinimized()
        if e.text() == "最大化":
            if self.isMaximized():
                self.showNormal()
                e.setIcon(
                    QApplication.style().standardIcon(QStyle.SP_TitleBarMaxButton)
                )
            else:
                self.showMaximized()
                e.setIcon(
                    QApplication.style().standardIcon(QStyle.SP_TitleBarNormalButton)
                )
        if e.text() == "关闭":
            self.close()

    def mousePressEvent(self, QMouseEvent):
        if QMouseEvent.button() == Qt.LeftButton:
            if self.isMaximized():
                return
            pos = QMouseEvent.globalPosition().toPoint()
            self.m_flag = True
            self.m_Position = pos - self.pos()  # 获取鼠标相对窗口的位置
            QMouseEvent.accept()
            self.setCursor(QCursor(Qt.OpenHandCursor))  # 更改鼠标图标

    def mouseMoveEvent(self, QMouseEvent):
        if QMouseEvent.buttons() == Qt.LeftButton and self.m_flag:
            if self.isMaximized():
                self.showNormal()
            pos = QMouseEvent.globalPosition().toPoint()
            self.move(pos - self.m_Position)  # 更改窗口位置
            QMouseEvent.accept()

    def mouseReleaseEvent(self, QMouseEvent):
        self.m_flag = False
        self.setCursor(QCursor(Qt.ArrowCursor))

    def paintEvent(self, event: QPaintEvent) -> None:
        opt = QStyleOption()
        opt.initFrom(self)
        p = QPainter(self)
        self.style().drawPrimitive(QStyle.PE_Widget, opt, p, self)
        return super().paintEvent(event)

    def eventFilter(self, watched: QObject, event: QEvent) -> bool:
        """自己定义事件过滤器"""
        # if event.type() == QEvent.MouseButtonPress:
        #     print(watched)
        #     if watched == self.Xbox.page_one.code:
        #         self.MSG_hasQrCode()
        #         return True
        return super().eventFilter(watched, event)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    mainwindow = MainWindow()
    mainwindow.show()
    sys.exit(app.exec())
